/*Program to compute inaction rates*/
/*Will also calculate inaction by size class and "weak" inaction (n +- 1%)*/
/*Revised April 2024*/

libname input "/OEUS/users/FEDSusers/QCEW/data/QuartData/";
libname output "/OEUS/users/FEDSusers/QCEW/revision_2024/Output/";
options  errors=1;

proc delete data=output.inactionrate_bysizegrp_allq;
proc delete data=output.inactionrate_bysizegrp_allq_wgt;

******************
*First compute size distribution;
proc univariate data=input.ldb20064;
weight m3_empl;
var m3_empl;
output out=output.pctiles pctlpre=P_ pctlpts=20,40,60,80;
run;

*Size classes of employment distribution in output.pctiles (20% of emps are in estabs less than 14);
*20th percentile: 14
*40th percentile: 42
*60th percentile: 116
*80th percentile: 381
*/;


%macro mac(YrBeg=,YrEnd=);

%do z=&YrBeg %to &YrEnd;

        %do j=1 %to 4;
                %let qrtyr=&z&j;

	**STOP IF AFTER 2015Q3;
	%if &j>3 and &z=2015 %then %do; %end;
	%else %do;

*x gzip -d "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&z&j..sas7bdat.gz";

data intermed (keep=ldb_num emp year qrt size);
                	set input.ldb&z&j;
            
			rename m3_empl=emp;
			
			if own_code~=5 then delete;
			if succ_break_consol_flag~=0 or pred_break_consol_flag~=0 then delete;
			
			
  			qrt=%eval(&j);
                	year=%eval(&z);
			*Size classes;
			if m3_empl>=1 and m3_empl<=14 then size=1;
			if m3_empl>14 and m3_empl<=42 then size=2;
			if m3_empl>42 and m3_empl<=116 then size=3;
			if m3_empl>116 and m3_empl<=381 then size=4;
			if m3_empl>381 then size=5;
			
			run;

proc sort data=intermed; by ldb_num;

*Merge forward 4 years of data;
%do zz=&z %to &z+4;

        %do jj=1 %to 4;

	    %if &zz=&z and &jj<=&j %then %do; %end;
	    %else %do;
	
                %let qrtyr=&zz&jj;
		
		%if &zz>=2016 %then %do; %end;
		%else %if &zz=2015 and &jj>=4 %then %do; %end;
		%else %do;

*  x gzip -d "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&zz&jj..sas7bdat.gz"; 

proc sort data=input.ldb&zz&jj; by ldb_num;
      data intermed2;
      merge intermed input.ldb&zz&jj (keep=ldb_num m3_empl own_code pred_break_consol_flag 
                		succ_break_consol_flag  rename=(m3_empl=emplst) );
      	   by ldb_num;
	   
	   	if own_code~=5 then delete;
		if pred_break_consol_flag~=0 or succ_break_consol_flag~=0 then delete;

  		qrtlst=%eval(&jj);
                yearlst=%eval(&zz);

		
		if emp=0 or emp=. or  emplst=0 or emplst=.  then delete;

		inactive = 0;
		if emp=emplst then inactive=1;
		if inactive = 1 then empavg = 0.5*(emp + emplst);

		*Use 1 emp,1% percent inaction band;
		inactive11 = 0;
		if emp>=min(emplst-1,floor(emplst*.99)) and emp<=max(emplst+1,ceil(emplst*1.01)) then inactive11 = 1;
		if inactive11 = 1 then empavg11 = 0.5*(emp + emplst);
		
		*Use 2 emp, 2% inaction band;
		inactive22 = 0;
		if emp>=min(emplst-2,floor(emplst*.98)) and emp<=max(emplst+2,ceil(emplst*1.02)) then inactive22 = 1;
		if inactive22 = 1 then empavg22 = 0.5*(emp+emplst);
	run;


*  x gzip "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&z&j..sas7bdat.gz";
*  x gzip "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&zz&jj..sas7bdat.gz"; 

proc means data=intermed2 noprint nway;
class size;
output out=inactionrate mean(inactive inactive11 inactive22 empavg empavg11 empavg22)=;
data inactionrate;set inactionrate;
qrt=%eval(&j);year=%eval(&z);
qrtlst=%eval(&jj);yearlst=%eval(&zz);
proc append base=output.inactionrate_bysizegrp_allq data=inactionrate;

****************WEIGHTED;
proc means data=intermed2 noprint nway;
class size;
weight emp;
output out=inactionrate_wgt mean(inactive inactive11 inactive22 empavg empavg11 empavg22)=;

data inactionrate_wgt;set inactionrate_wgt;
qrt=%eval(&j);year=%eval(&z);
qrtlst=%eval(&jj);yearlst=%eval(&zz);

proc append base=output.inactionrate_bysizegrp_allq_wgt data=inactionrate_wgt;



run;

%end;
%end;
%end;
%end;
%end;
%end;
%end;
%mend;


*Iterate through all years to compile data, calculate inaction, delete;
%mac(YrBeg=1990,YrEnd=2015);	


